import * as request from "../../../utils/request/user/personManage"
Page({
  data: {
    //value: "",
    personnelList: [], // 初始人员列表（空数组或预加载部分数据）  
    loadingMore: false, // 加载更多状态  
    hasMore: true, // 是否有更多数据  
    pageIndex: 1, // 当前页码  
    pageSize: 10, // 每页显示的数据条数（根据iPhone 12的屏幕大小调整）  
    editDialogVisible: false,
    info: null,
    isSearch:false,
    firstQuery: true,
    loadText: "加载更多...",
    userInfoOptions: [
      '用户名',
      "角色",
    ],
    index: 0,
    selectedUserInfo: "请选择根据哪一属性进行搜索"
  },

  onLoad: function () {
    this.setData({
      pageIndex: 1
    })
    this.loadPersonnelData(); // 页面加载时获取初始数据  
  },

  onReachBottom: function () {
    //console.log(this.data.hasMore)
    if (!this.data.hasMore) {
      this.setData({
        loadingMore: true,
        loadText: "已经到底了"
      });
      setTimeout(() => {
        this.setData({
          loadingMore: false
        })
      }, 800)
      return;
    }
    this.setData({
      loadingMore: true
    });
    this.data.isSearch?this.getSearchRes(): this.loadMorePersonnelData()
  },

  loadPersonnelData: async function (isSearch) {

    let session = wx.getStorageSync('session')
    const start = this.data.pageIndex;
    const end = this.data.pageSize;
    let data, res
    data = {
      current: start,
      pageSize: end,
    }

    data = {
      current: start,
      pageSize: end
    }
    res = await request.userList(data, session)
    // console.log(start)
     console.log(res)
    let arr = this.data.personnelList
    for (let i = 0; i < res.data.records.length; i++) {
      arr.push(res.data.records[i])
    }
    //console.log(arr)
    this.setData({
      personnelList: arr,
      hasMore: res.data.total > this.data.personnelList.length, // 判断是否还有更多数据  
      loadingMore: false,
    });

  },

  loadMorePersonnelData: function () {

    this.data.pageIndex++;
    this.loadPersonnelData(); // 调用加载数据的函数，实际上是重新加载了当前页码的数据并追加到列表中（这里为了简化没有实现真正的追加操作）  

  },
  editPersonnel(e) {
    this.setData({
      editDialogVisible: true,
    });
    let id = e.currentTarget.dataset.id
    let target = null;

    for (const item of this.data.personnelList) {
      if (item.id == id) {
        target = item
        break;
      }
    }

    this.setData({
      info: target
    })
    console.log(wx.getStorageSync('userInfo').data.id, e.currentTarget.dataset.id)
  },
  deletePersonnel: async function (e) {
    const id = e.currentTarget.dataset.id;

    let session = wx.getStorageSync('session')
    let data = {
      id
    }
    let res = await request.deleteUser(data, session)
    if (res.code === 0) {
      wx.showToast({
        title: '封禁成功',
        icon: 'none'
      });
      let theId = wx.getStorageSync('userInfo').data.id
      if (id == theId) {
        wx.removeStorage({
          key: 'userInfo',
        })
        wx.removeStorage({
          key: 'session',
        })
        wx.redirectTo({
          url: '/pages/login/index'
        });
        return
      }
      this.loadPersonnelData()
    } else {
      wx.showToast({
        title: '封禁失败',
        icon: 'none'
      });
    }
    // this.setData({  
    //   personnelList: newPersonnelList,  
    //   // 如果删除的是最后一页的最后一条数据，需要更新hasMore状态（这里简化处理，不更新）  
    // });  

  },


  onUnblock(e) {
    // 处理解封逻辑  
  },

  onCancelDialog(e) {
    this.setData({
      editDialogVisible: false
    });
  },

  async onConfirmDialog(e) {

    let data = e.detail.data;

    let session = wx.getStorageSync('session')
    //  console.log(data)
    // 处理确认逻辑，使用 role 数据  
    let res = await request.updateUser(data, session)
    //console.log(res)
    if (res.code === 0) {
      this.setData({
        current:1,
        personnelList:[]
      })
      await this.loadPersonnelData();
      this.setData({
        editDialogVisible: false
      });
      wx.showToast({
        title: '修改成功',
        icon: "none"
      })
    } else {
      wx.showToast({
        title: '修改失败',
        icon: "none"
      })
    }


  },
  async getSearchRes() {
    let session = wx.getStorageSync('session')
    const start = this.data.pageIndex;
    const end = this.data.pageSize;
    let data, res
    data = {
      current: start,
      pageSize: end,
    }
    data.userAccount = this.data.value
    res = await request.userList(data, session)
    if (this.data.firstQuery) {
      this.setData({
        personnelList: res.data.records,
        firstQuery: false
      })
      return
    }
    let arr = this.data.personnelList
    for (let i = 0; i < res.data.records.length; i++) {
      arr.push(res.data.records[i])
    }
    //console.log(arr)
    this.setData({
      personnelList: arr,
      hasMore: res.data.total > this.data.personnelList.length, // 判断是否还有更多数据  
      loadingMore: false,
    });

  },
  onClick() {
    
    this.setData({
      pageIndex:1,
      isSearch:true,
      firstQuery:true,
      personnelList:[]
    })
   
    this.getSearchRes()
  },
  onChange(e) {
    this.setData({
      value: e.detail
    })
  },

  // bindPickerChange: function (e) {
  //   this.setData({
  //     index: e.detail.value,
  //     selectedUserInfo: this.data.userInfoOptions[e.detail.value]
  //   });

  // }
});